#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
将每一行是list的文件转为csv
'''
import sys
import csv
import json

def file_to_csv(abs_file_path):
        with open(abs_file_path,'r') as f,open(abs_file_path+'.csv', 'w') as csvfile:
            writer = csv.writer(csvfile, delimiter=',', dialect='excel')
            for line in f:
                line = line.strip()
                row_values = []
                try:
                    row_values = json.loads(line)
                    if not isinstance(row_values,list):
                        raise Exception('{} is not list'.format(row_values))
                except Exception as e:
                    print("{} can't to json\t{}".format(line,e))
                if row_values:
                    writer.writerow(row_values)
'''
# 获取命令行参数列表： 
root@transformer-hunter:/tmp/dsf# python xx.py v1_mobile_api
['xx.py', 'v1_mobile_api']
第一个值默认是当前py文件名
'''

inp = sys.argv
print(inp)
file_name = inp[1]
file_to_csv(file_name)
print('转为csv success at{}'.format(file_name))
